# !/usr/bin/env python
# coding=utf-8
# @Time    : 2018/7/16 13:12
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/rpc_dao.git
# @File    : insurance_types.py
# @Software: PyCharm
# @Version: 2.7.5
from db import insurance_types
from idl_output.ttypes import InsuranceTypes, InsuranceUnit
from utils import my_decorator


@my_decorator.handle_exception()
def get_insurance_types():
	orm_data = insurance_types.get_insurance_types()
	thrift_data = []
	for i in orm_data:
		thrift_data.append(InsuranceTypes(i.id, i.code, i.type_name,
		                                  i.amount_range, i.description, i.status))
	return thrift_data


@my_decorator.handle_exception()
def get_insurance_units(provinceCode, cityCode, appId):
	province_id, city_id = insurance_types.get_gbcode_id(cityCode)
	orm_data = insurance_types.get_insurance_units(province_id, city_id, appId)
	if len(orm_data) > 0:
		thrift_data = []
		for i in orm_data:
			thrift_data.append(InsuranceUnit(id=i.id, logoURL=i.logoURL, unit_name=i.unit_name,
			                                  unit_code=i.unit_code))
		return thrift_data
	else:
		# 其它地市如果没有单独配置保险公司，默认获取省会城市的保险公司
		return _get_default_insurance_unit(appId, cityCode)


def _get_default_insurance_unit(appId, cityCode):
	province_id, city_id = insurance_types.get_gbcode_id(cityCode[0:2] + '0100')
	orm_data = insurance_types.get_insurance_units(province_id, city_id, appId)
	thrift_data = []
	for i in orm_data:
		thrift_data.append(InsuranceUnit(id=i.id, logoURL=i.logoURL, unit_name=i.unit_name,
		                                 unit_code=i.unit_code))
	return thrift_data


@my_decorator.handle_exception()
def get_rate1(appId, cityCode, insuranceCode):
	rate1 = insurance_types.get_commission_rate(appId, cityCode, insuranceCode)
	if rate1 == '0.00':
		rate1 = insurance_types.get_commission_rate(appId, cityCode[0:2] + '0100', insuranceCode)
	return rate1



